<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="50%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
             @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="内容" prop="content" style="width: 100%">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="链接" prop="urlStr" style="width: 100%">
        <el-input v-model="dataForm.urlStr" placeholder="链接"></el-input>
      </el-form-item>
      <el-form-item label="类别" size="mini" prop="type" style="width: 100%">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="0">广告</el-radio>
          <el-radio :label="1">警告</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="时间范围" prop="timeRange" style="width: 100%">
        <el-date-picker
          v-model="dataForm.timeRange"
          type="datetimerange"
          :picker-options="pickerOptions"
          @change="timeRangeChange"
          format="yyyy-MM-dd HH:mm"
          value-format="yyyy-MM-dd HH:mm"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          content: '',
          urlStr: '',
          type: 0,
          beginTime: '',
          endTime: '',
          timeRange: '',
          deleted: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          content: [
            {required: true, message: '内容不能为空', trigger: 'blur'}
          ],
          timeRange: [
            {required: true, message: '时间范围不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sto/content/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.content = data.content.content
                this.dataForm.urlStr = data.content.urlStr
                this.dataForm.type = data.content.type
                // this.dataForm.beginTime = data.content.beginTime
                // this.dataForm.endTime = data.content.endTime
                this.dataForm.timeRange = [data.content.beginTime, data.content.endTime]
                this.dataForm.deleted = data.content.deleted
                this.dataForm.createUserId = data.content.createUserId
                this.dataForm.createTime = data.content.createTime
              }
            })
          } else {
            this.dataForm.content = ''
            this.dataForm.type = 0
            this.dataForm.timeRange = ''
            this.dataForm.beginTime = ''
            this.dataForm.endTime = ''
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sto/content/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'content': this.dataForm.content,
                'urlStr': this.dataForm.urlStr,
                'type': this.dataForm.type,
                'beginTime': this.dataForm.beginTime,
                'endTime': this.dataForm.endTime,
                // 'timeRange': this.dataForm.timeRange,
                'deleted': this.dataForm.deleted,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      timeRangeChange (val) {
        // this.dataForm.beginTime = new Date(val[0])
        // this.dataForm.endTime = new Date(val[1])
        this.dataForm.beginTime = val[0]
        this.dataForm.endTime = val[1]
        console.log(this.dataForm.beginTime)
        console.log(this.dataForm.endTime)
      }
    }
  }
</script>
